﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using WorldMapBase;
using WorldMapBase.Tile.Pool;
using Yoozoo.Managers.ResourceManagerV2.Runtime;
using Object = UnityEngine.Object;

namespace Yoozoo.Gameplay.RTS
{
    public class HudPool : ObjectPool<GameObject>
    {
        private GameObject prefab;
        private ResLoader _resLoader;
        private string assetPath;
        private Transform root;
        public bool InitComplete = false;
        private int type;
        public int initNum;
        public HudPool(int initNum, int capacity, string assetPath, Transform poolRoot,
            bool enableAutoShrink, int idleMaxCount, int frameShrinkCount,float shrinkIntervalTime,int type,bool withCanvas=false,bool withGRay=false) : 
            
            base(capacity,enableAutoShrink,idleMaxCount,frameShrinkCount,shrinkIntervalTime)
        {
#if UNITY_EDITOR
            var strs = assetPath.Split('/');
            _name = "RtsHudPool: " +  strs[strs.Length - 1];
#endif
            
            this.assetPath = assetPath;
            this.type = type;
            _resLoader = ResLoader.Alloc();
            this.initNum = initNum;

            string rootName = "[ObjectPool]";
#if UNITY_EDITOR
            rootName += System.IO.Path.GetFileNameWithoutExtension(assetPath);
#endif
            var rootObject = new GameObject(rootName);
            if (withCanvas)
            {
                var canvas = rootObject.AddComponent<Canvas>();
                if (withGRay)
                {
                    rootObject.AddComponent<GraphicRaycaster>();   
                }
                root = rootObject.transform;
                root.SetParent(poolRoot);
            }
            else
            {
                root = rootObject.transform;
                root.SetParent(poolRoot);
            }

        }

        public void StartLoad(int initNum,Action<string, bool> funcCallBack)
        {
            _resLoader.Add2Load(assetPath, (success, name, asset) =>
            {
                if (success)
                {
                    prefab = asset as GameObject;
                    OnInitAsync(initNum);
                    InitComplete = true;

                }
                if (funcCallBack != null)
                {
                    funcCallBack.Invoke(assetPath, success);
                }
            }).Load();
        }
    

        protected override GameObject ConstructItem()
        {
            if (prefab == null)
            {
                Debug.LogErrorFormat("Prefab {0} is not ready", assetPath);
                return null;
            }

            var go = Object.Instantiate(prefab, root);
            go.transform.position = GlobalValue.INVISIBLE_POSITION;
            go.SetActive(true);
            return go;
        }
        private List<Animator> tempAnimatorList = new List<Animator>(4);
        protected override void OnGetObject(GameObject obj)
        {
            base.OnGetObject(obj);
            tempAnimatorList.Clear();
            obj.GetComponentsInChildren<Animator>(true, tempAnimatorList);
            if (tempAnimatorList.Count > 0)
            {
                for (int i = 0; i < tempAnimatorList.Count; i++)
                {
                    tempAnimatorList[i].enabled = true;
                }
            }

            var canvasGroup = obj.GetComponent<CanvasGroup>();
            if(canvasGroup!=null)canvasGroup.alpha = 1;
        }
        protected override void BeforeRecover(GameObject obj)
        {
            var objTransform = obj.transform;
            objTransform.SetParent(root);
            objTransform.position = GlobalValue.INVISIBLE_POSITION;

            objTransform.GetComponentsInChildren<Animator>(tempAnimatorList);
            if (tempAnimatorList.Count > 0)
            {
                for (int i = 0; i < tempAnimatorList.Count; i++)
                {
                    tempAnimatorList[i].enabled = false;
                }
            }
            var canvasGroup = obj.GetComponent<CanvasGroup>();
            if(canvasGroup!=null)canvasGroup.alpha = 0;
        }

        public void Dispose()
        {
            if (_resLoader != null)
            {
                _resLoader.Recycle2Cache();
                _resLoader = null;
            }

            prefab = null;
            Destroy();
            if (root != null)
            {
                Object.Destroy(root.gameObject);
                root = null;
            }
            tempAnimatorList?.Clear();
        }
    }
}
